package cn.newgrand.netcall.consultant.components;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import org.jivesoftware.spark.SparkManager;

import cn.newgrand.netcall.consultant.SpecialistQueueManager;
import cn.newgrand.netcall.consultant.ui.MessageCenterPanel;
import cn.newgrand.smackx.consultant.ConsultantManager;
import cn.newgrand.smackx.consultant.SpidJIDMap;
import cn.newgrand.smackx.consultant.packet.SpecialistNotifyPacket;

public class ButtonCellEditor extends AbstractCellEditor implements
		TableCellEditor,TableCellRenderer {
	private String join = "加入";
	private String exit = "退出";
	private Map<Integer,JButton> buttons;
	public ButtonCellEditor() {
		super();
		// TODO Auto-generated constructor stub
		buttons = new HashMap<Integer, JButton>();

	}

	@Override
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return "";
	}

	@Override
	public Component getTableCellEditorComponent(final JTable table, Object value,
			boolean isSelected, final int row, int column) {
		// TODO Auto-generated method stub
		//this.table = table;
		//this.row = row;
		//return button;
		if(buttons.containsKey(row)){
			return buttons.get(row);
		}
		
		final JButton button; //= new JButton(join);
		if(SpecialistQueueManager.getInstance().isJoinQueue((String) table.getValueAt(row, 1))){
			button = new JButton(exit);
		}else{
			button = new JButton(join);
		}
		buttons.put(row, button);
		button.setOpaque(true);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(table!=null){
					
					if(join.equals(button.getText())){
						SpecialistNotifyPacket result = ConsultantManager.getInstance(SparkManager.getConnection()).joinSpecialistQueue((String) table.getValueAt(row, 1));
			    		SparkManager.getMainWindow().requestFocus();
						if(result.getStatus().equals("yes")){
							SpecialistQueueManager.getInstance().putWhenJoin((String) table.getValueAt(row, 1));
			    			JOptionPane.showMessageDialog(null, "加入成功");
							button.setText(exit);
			    		}
			    		else{
			    			if(result.getResult().equals("busy"))
			    				JOptionPane.showMessageDialog(null, "专家正忙，队列已满");
			    			else if(result.getResult().equals("repeat"))
			    				JOptionPane.showMessageDialog(null, "重复加入队列");
			    			else if(result.getResult().equals("sameone"))
			    				JOptionPane.showMessageDialog(null, "不能加入自己队列");
			    			else
			    				JOptionPane.showMessageDialog(null, "无法加入队列");
							button.setText(join);
			    		}
					}else if(exit.equals(button.getText())){
						String spid = (String) table.getValueAt(row, 1);
						if(MessageCenterPanel.getInstance().isHasSpecialistMessage(spid)){
							MessageCenterPanel.getInstance().removeJoinMessageItem(spid, SpidJIDMap.getJID(spid));
							SparkManager.getMainWindow().requestFocus();
							button.setText(join);
						}else{
							JOptionPane.showMessageDialog(null, "不在当前专家队列中");
							button.setText(join);
						}
					}
				}
			}
		});
		return button;
	}

//	@Override
//	public void actionPerformed(ActionEvent e) {
//		// TODO Auto-generated method stub
//		//发送加入队列请求
//		if(this.table!=null){
//			
//			if(join.equals(button.getText())){
//				SpecialistNotifyPacket result = ConsultantManager.getInstance(SparkManager.getConnection()).joinSpecialistQueue((String) this.table.getValueAt(this.row, 1));
//	    		SparkManager.getMainWindow().requestFocus();
//				if(result.getStatus().equals("yes")){
//	    			JOptionPane.showMessageDialog(null, "加入成功");
//					this.button.setText(exit);
//	    		}
//	    		else{
//	    			if(result.getResult().equals("busy"))
//	    				JOptionPane.showMessageDialog(null, "专家正忙，队列已满");
//	    			else if(result.getResult().equals("repeat"))
//	    				JOptionPane.showMessageDialog(null, "重复加入队列");
//	    			else if(result.getResult().equals("sameone"))
//	    				JOptionPane.showMessageDialog(null, "不能加入自己队列");
//	    			else
//	    				JOptionPane.showMessageDialog(null, "无法加入队列");
//					this.button.setText(join);
//	    		}
//			}else if(exit.equals(button.getText())){
//				String spid = (String) this.table.getValueAt(this.row, 1);
//				if(MessageCenterPanel.getInstance().isHasSpecialistMessage(spid)){
//					MessageCenterPanel.getInstance().removeJoinMessageItem(spid, SpidJIDMap.getJID(spid));
//					SparkManager.getMainWindow().requestFocus();
//					this.button.setText(join);
//				}else{
//					JOptionPane.showMessageDialog(null, "不在当前专家队列中");
//					this.button.setText(join);
//				}
//			}
//		}
//	}

	@Override
	public Component getTableCellRendererComponent(final JTable table, Object value,
			boolean isSelected, boolean hasFocus,final int row, int column) {
		// TODO Auto-generated method stub
		if(buttons.containsKey(row)){
			return buttons.get(row);
		}
		
		final JButton button; //= new JButton(join);
		if(SpecialistQueueManager.getInstance().isJoinQueue((String) table.getValueAt(row, 1))){
			button = new JButton(exit);
		}else{
			button = new JButton(join);
		}
		buttons.put(row, button);
		button.setOpaque(true);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(table!=null){
					
					if(join.equals(button.getText())){
						SpecialistNotifyPacket result = ConsultantManager.getInstance(SparkManager.getConnection()).joinSpecialistQueue((String) table.getValueAt(row, 1));
			    		SparkManager.getMainWindow().requestFocus();
						if(result.getStatus().equals("yes")){
							SpecialistQueueManager.getInstance().putWhenJoin((String) table.getValueAt(row, 1));
			    			JOptionPane.showMessageDialog(null, "加入成功");
							button.setText(exit);
			    		}
			    		else{
			    			if(result.getResult().equals("busy"))
			    				JOptionPane.showMessageDialog(null, "专家正忙，队列已满");
			    			else if(result.getResult().equals("repeat"))
			    				JOptionPane.showMessageDialog(null, "重复加入队列");
			    			else if(result.getResult().equals("sameone"))
			    				JOptionPane.showMessageDialog(null, "不能加入自己队列");
			    			else
			    				JOptionPane.showMessageDialog(null, "无法加入队列");
							button.setText(join);
			    		}
					}else if(exit.equals(button.getText())){
						String spid = (String) table.getValueAt(row, 1);
						if(MessageCenterPanel.getInstance().isHasSpecialistMessage(spid)){
							MessageCenterPanel.getInstance().removeJoinMessageItem(spid, SpidJIDMap.getJID(spid));
							SparkManager.getMainWindow().requestFocus();
							button.setText(join);
						}else{
							JOptionPane.showMessageDialog(null, "不在当前专家队列中");
							button.setText(join);
						}
					}
				}
			}
		});
		return button;
	}
}
